<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.png?v=5.1.4">


  <link rel="mask-icon" href="/images/favicon.png?v=5.1.4" color="#222">





  <meta name="keywords" content="模式识别," />





  <link rel="alternate" href="/atom.xml" title="平步青云win" type="application/atom+xml" />






<meta name="description" content="物以俱类，人以群分">
<meta name="keywords" content="模式识别">
<meta property="og:type" content="article">
<meta property="og:title" content="模式识别(二)--聚类分析">
<meta property="og:url" content="https://zxpgo.github.io/2018/09/11/聚类分析/index.html">
<meta property="og:site_name" content="平步青云win">
<meta property="og:description" content="物以俱类，人以群分">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://i.imgur.com/whme94d.png">
<meta property="og:image" content="https://i.imgur.com/VhVHTyV.png">
<meta property="og:image" content="https://i.imgur.com/Eguul2a.png">
<meta property="og:image" content="https://i.imgur.com/PVtenpJ.png">
<meta property="og:image" content="https://i.imgur.com/BHa0iXW.png">
<meta property="og:updated_time" content="2018-11-08T11:10:35.180Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="模式识别(二)--聚类分析">
<meta name="twitter:description" content="物以俱类，人以群分">
<meta name="twitter:image" content="https://i.imgur.com/whme94d.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"right","display":"post","offset":10,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'PAO8LM7QB1',
      apiKey: '',
      indexName: 'Blog',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zxpgo.github.io/2018/09/11/聚类分析/"/>





  <title>模式识别(二)--聚类分析 | 平步青云win</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?7a4517a3ce6d7c50203655d056f01ac3";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-right page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">平步青云win</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-随笔">
          <a href="/sui" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            随笔
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
			
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zxpgo.github.io/2018/09/11/聚类分析/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="zxp">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="平步青云win">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">模式识别(二)--聚类分析</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-11T22:28:50+08:00">
                2018-09-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/模式识别/" itemprop="url" rel="index">
                    <span itemprop="name">模式识别</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/09/11/聚类分析/" class="leancloud_visitors" data-flag-title="模式识别(二)--聚类分析">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <p>物以俱类，人以群分<a id="more"></a></p>
<h2 id="2-1-距离聚类的概念"><a href="#2-1-距离聚类的概念" class="headerlink" title="2.1 距离聚类的概念"></a>2.1 距离聚类的概念</h2><h3 id="物以俱类"><a href="#物以俱类" class="headerlink" title="物以俱类"></a>物以俱类</h3><p>”物以俱类，人以群分“，反映了聚类分析的基本思想。</p>
<p>聚类分析属于非监督分类，即基本上无先验知识可依据或参考。</p>
<p>聚类分析根据模式之间的相似性对模式进行分类，对一批没有标出类别的模式样本集，将相似的归为一类，不相似的归为一类。</p>
<h3 id="相似性"><a href="#相似性" class="headerlink" title="相似性"></a>相似性</h3><p>当研究一个复杂对象时，可以对其特征进行各种可能的测量，将测量值组成向量形式，称为该样本的特征向量，有$n$个特征值组成的就是$n$维向量，相当于空间中的一个点，整个模式样本集的特征向量可以看作分布在特征空间中的一些点。</p>
<p>可以将特征空间中点与点之间的距离函数作为模式相似性的测量，以“距离”作为模式分类的依据，距离越小，越相似。</p>
<h2 id="2-2-相似度测量和距离准则"><a href="#2-2-相似度测量和距离准则" class="headerlink" title="2.2 相似度测量和距离准则"></a>2.2 相似度测量和距离准则</h2><h3 id="2-2-1-相似性测度"><a href="#2-2-1-相似性测度" class="headerlink" title="2.2.1 相似性测度"></a>2.2.1 相似性测度</h3><p>相似性测度是衡量模式之间相似性的一种尺度。距离就是一种相似性的测度，他可以用来度量同一类模式之间的类似性和不属于同一类的模式之间的差异性。下面介绍几种距离：</p>
<p><strong>1.欧式距离</strong></p>
<p>设$X_i,X_j$为两个$n$维模式，$X_i=(x_{i1},x_{i2}, \dots, x_{in})^T$，$X_j=(x_{j1},x_{j2}, \dots, x_{jn})^T$，则定义欧式距离为：</p>
<p>$D(X_i,X_j)=|||X_i-X_j||=\sqrt{(X_i-X_j)^T(X_i-X_j)}=\sqrt{(x_{i1}-x_{j1})^2+\dots+(x_{in}-x_{jn})^2}$</p>
<p><strong>2.马氏距离</strong></p>
<p>马氏距离常用平方形式表示。设$X$为模式向量，$M$为某类模式的均值向量，$C$为该类模式总体的协方差矩阵，则马氏距离定义为：</p>
<p>$D^2 = (X-M)^TC^{-1}(X-M)$</p>
<p>对$n$为向量可表示为：$X=(x_{1},x_{2}, \dots, x_{n})^T$,$M=(m_{1},m_{2}, \dots, m_{n})^T$,$C=E{(X-M)(X-M)^T}。</p>
<p><img src="https://i.imgur.com/whme94d.png" alt=""></p>
<p>协方矩阵C表示的是各分量上模式样本到均值的距离，也就是在各维上模式的分散情况。</p>
<p><strong>3.明氏距离</strong></p>
<p>设$X_i, X_j$为$n$维模式向量，$X_i, X_j$之间的明氏距离$D_m$表示为：</p>
<p>$D_m(X_i,X_j)=[\sum_{k=1}^n|x_{ik}-x_{jk}|^m]^{\frac{1}{m}}$</p>
<p>当$m=2$时，明氏距离就是欧式距离。<br>当$m=1$时，可得：</p>
<p>$D_1(X_i,X_j)=\sum_{k=1}^n|x_{ik}-x_{jk}|$</p>
<p>称其为“街坊”距离。</p>
<p><img src="https://i.imgur.com/VhVHTyV.png" alt=""></p>
<p><strong>4.汉明距离</strong></p>
<p>如果模式向量各分量的值仅取1或（-1），即二值模式，则可用汉民距离衡量模式间的相似性。设$X_i, X_j$为$n$维二值模式向量，$X_i, X_j$之间的汉明距离$D_m$表示为：</p>
<p>$D_h(X_i,X_j)=\frac{1}{2}(n-\sum_{k=1}^nx_{ik} \cdot x_{jk})$</p>
<p>由定义可知，若两个模式向量的每个分量取值都不同，则汉明距离为n；若两个模式向量的各分量取值都相同，则汉明距离为零。</p>
<p><strong>5.角度相似性函数</strong></p>
<p>角度相似性函数表示为：</p>
<p><img src="https://i.imgur.com/Eguul2a.png" alt=""></p>
<p>它是模式之间的余弦。</p>
<h3 id="2-2-2-聚类准测"><a href="#2-2-2-聚类准测" class="headerlink" title="2.2.2 聚类准测"></a>2.2.2 聚类准测</h3><p>聚类准则：根据相似性测度确定的，衡量模式之间是否相似的标准。即把不同模式聚为一类还是归为不同类的准则。</p>
<p>确定聚类准则的两种方式：</p>
<ul>
<li>1.阈值准则：根据规定的距离阈值进行分类的准则。</li>
<li>2.函数准则：利用聚类准则函数进行分类的准则。</li>
</ul>
<p>聚类准则函数：在聚类分析中，表示模式类间相似或差异性的函数。</p>
<p>一种常用的指标是误差平方之和，聚类准则函数定义如下：</p>
<p>$J = \sum_{j=1}^c \sum_{x \in S_j} ||X-M_j||^2$</p>
<p>其中，$c$表示共有$c$个模式类；$M_j = \frac{1}{N_j} \sum_{X \in S_j}X$,为$S_j$中模式样本的均值向量，$N_j$为$S_j$中的模式样本数量。</p>
<p>J代表了分属于c个聚类类别的全部模式样本与其相应类别模式均值之间的误差平方和。 </p>
<h2 id="2-3-基于距离阈值的聚类算法"><a href="#2-3-基于距离阈值的聚类算法" class="headerlink" title="2.3 基于距离阈值的聚类算法"></a>2.3 基于距离阈值的聚类算法</h2><h3 id="2-3-1-近邻聚类算法"><a href="#2-3-1-近邻聚类算法" class="headerlink" title="2.3.1 近邻聚类算法"></a>2.3.1 近邻聚类算法</h3><h4 id="问题："><a href="#问题：" class="headerlink" title="问题："></a>问题：</h4><p>有N个待分类的模式$\lbrace X_1,X_2, \dots, X_N \rbrace$，要求按距离阈值T分类到以$Z_1, Z_2,\dots$为聚类中心的模式类中。</p>
<h4 id="算法描述"><a href="#算法描述" class="headerlink" title="算法描述"></a>算法描述</h4><p>1、任取样本Xi作为第一个聚类中心的初始值，如令$Z_1 = X_1$。</p>
<p>2、算样本X2到Z1的欧式距离$D_{21}= ||X_2 - Z_1||$，若$D_{21}&gt;T$，定义一新的聚类中心$Z_2 = X_2%；否则$X2 \in$以$Z_1$为中心的聚类。</p>
<p>3、假设已有聚类中心$Z_1,Z_2$，计算$D_{31}=||X_3 - Z_1||$和$D_{32}=||X_3 - Z_2||$，若$D_{31}&gt;T$且$D_{32}&gt;T$，则建立第三个聚类中心$Z_3 = X_3$；否则$X3 \in$离$Z_1$和$Z_2$中最近着（最近邻的聚类中心）。</p>
<p>……以此类推，直到将所有的N个样本都进行分类。</p>
<h4 id="算法特点"><a href="#算法特点" class="headerlink" title="算法特点"></a>算法特点</h4><p>局限性：很大程度上依赖于第一个聚类中心的位置选择、待分类模式样本的排列次序、距离阈值T的大小以及样本分布的几何性质等。</p>
<p>优点：计算简单。（一种虽粗糙但快速的方法）</p>
<h3 id="2-3-2-最大最小距离算法"><a href="#2-3-2-最大最小距离算法" class="headerlink" title="2.3.2 最大最小距离算法"></a>2.3.2 最大最小距离算法</h3><h4 id="问题：-1"><a href="#问题：-1" class="headerlink" title="问题："></a>问题：</h4><p>有N个待分类的模式$\lbrace X_1,X_2, \dots, X_N \rbrace$，要求按距离阈值T分类到以$Z_1, Z_2,\dots$为聚类中心的模式类中。</p>
<h4 id="算法描述-1"><a href="#算法描述-1" class="headerlink" title="算法描述"></a>算法描述</h4><p>1、任取样本Xi作为第一个聚类中心的初始值，如令$Z_1 = X_1$。</p>
<p>2、选择离$Z_1$距离最远的模式样本作为第二个聚类中心$Z_2$。</p>
<p>3、逐个计算每个模式样本与已确定的所有聚类中心之间的距离，并选出其中最小距离。 </p>
<p>例当聚类中心数k=2时，计算</p>
<p>$D_{i1} = ||X_i-Z_1||$,  $D_{i2} = ||X_i-Z_2||$</p>
<p>并求出：</p>
<p>$\min{(D_{i1},D_{i2})}$</p>
<p>因为共有N个模式样本，所以此时得到N个最小距离。</p>
<p>4、在所有最小距离中选出一个最大距离，如果该最大值达到$||Z_1-Z_2||$的一定分数以上，则将产生最大距离的那个模式样本定义定义为<strong>新增的聚类中心</strong>，并返回上一步。否则，聚类中心的计算步骤结束。这里的“$||Z_1-Z_2||$的一定分数比值”就是阈值$T$,即</p>
<p>$T = \theta ||Z_1-Z_2||$</p>
<p>同样，即$k=2$为例，若$\max{\lbrace \min{(D_{i1},D_{i2})}, i=1,2,\dots, N \rbrace} &gt; T$，则$Z_3$存在，并取为相应的模式向量，返回步骤三；否则寻找聚类中心结束。</p>
<p>5、重复步骤3和步骤4，直到没有聚类中心出现为止。</p>
<p>6、寻找聚类中心的运算结束后，将模式样本$\lbrace X_i, i =1,2,\dots,N \rbrace$按最近距离划分到相应聚类中心所代表的类别中。</p>
<h2 id="2-4-层次聚类"><a href="#2-4-层次聚类" class="headerlink" title="2.4 层次聚类"></a>2.4 层次聚类</h2><p>层次聚类法也称为系统聚类法或分级聚类法。这种方法同样采用距离与之作为决策聚类数目的标准，基本思路：每个样本先自成一类，然后按距离准则逐步合并，减少类数，直到达到分类要求为止。</p>
<h4 id="算法描述-2"><a href="#算法描述-2" class="headerlink" title="算法描述"></a>算法描述</h4><p>1、N个初始化模式样本自成一类，即建立N类$G_1(0),G_2(0),\dots,G_N(0)$。计算各类之间（各样本间）的距离，得到一个$N \times N$维的距离矩阵$D(0)$。标号$(0)$表示是聚类开始运算前的状态。</p>
<p>2、如在前一步聚类运算中，以求得距离矩阵$D(n)$(n为逐次聚类合并得次数)，则找出$D(n)$中得最小元素，将其对应得两类合并为一类。由此建立新的分类：$G_1(n+1),G_2(n+1),\dots$。</p>
<p>3、计算合并后新类别之间得距离，得到距离矩阵$D(n+1)$。</p>
<p>4、 跳至第2步，重复计算及合并。</p>
<p>结束条件：设定一个距离阈值$T$，当$D(n)$的最小值分量超过给定值$T$时，算法停止。</p>
<p>在第3步中，计算合并后的聚类与其他没有合并的模式类之间的距离，或者两个合并后的聚类之间的距离时，应如何计算？下面介绍几种不同的类间距离计算准则：</p>
<ul>
<li>（1）最短距离法</li>
<li>（2）最长距离法</li>
<li>（3）中间距离法</li>
<li>（4）重心法</li>
<li>（5）类平均距离法</li>
</ul>
<h2 id="2-5-动态聚类法"><a href="#2-5-动态聚类法" class="headerlink" title="2.5 动态聚类法"></a>2.5 动态聚类法</h2><p>动态聚类法首先选择若干个模式样本作为聚类中心，再按照实现确定的聚类准则进行聚类。在聚类过程中，根据聚类准则对聚类中心进行反复修改，直到分类合理为止。</p>
<h3 id="2-5-1-K-均值算法"><a href="#2-5-1-K-均值算法" class="headerlink" title="2.5.1 K-均值算法"></a>2.5.1 K-均值算法</h3><p>K-均值算法又称为C-均值算法，时根据函数准则进行分类的聚类算法，基于使聚类准则函数最小化。</p>
<p><img src="https://i.imgur.com/PVtenpJ.png" alt=""></p>
<h4 id="算法描述-3"><a href="#算法描述-3" class="headerlink" title="算法描述"></a>算法描述</h4><p>设共有$N$个模式样本，计算步骤如下：</p>
<p>1、 任选$K$个初始化聚类中心$Z_1(1),Z_1(2),Z_K(1), K &lt; N$。括号内的序号代表寻找聚类中心的迭代次序号。一般选择样本集中前$K$个样本作为初始聚类中心。</p>
<p>2、 按<strong>最短距离原则</strong>将其余样本分配到$K$个聚类中心中的某一个，即</p>
<p>若 $\min{\lbrace ||X-Z_i(k)||, i =1,2,\dots, K\rbrace} = ||X-Z_j(k)||=D_j(k)$<br>则，$X \in S_j(k)$</p>
<p>式中，$k$即为迭代运算的次序号。</p>
<p>3、计算各个聚类中心的新向量值$Z_j(k+1),j =1,2,\dots,K$</p>
<p>$Z_j(k+1) = \frac{1}{N_j} \sum_{X \in S_j(k)}X, j = 1,2,\dots, K$</p>
<p>即以均值向量作为新的聚类中心。这一步要分别计算$K$个聚类中的样本均值向量，故称算法为K-均值聚类。</p>
<p>4、如果$Z_j(k+1) \neq Z_j(k),j =1,2,\dots,K$，则回到步骤2，将模式样本逐个重新分类，并重复迭代计算，如果$Z_j(k+1) = Z_j(k),j =1,2,\dots,K$，算法收敛，计算完毕。</p>
<p>K均值聚类算法的思想是：首先随机选取K个对象作为初始的聚类中心，然后，计算每个对象距聚类中心的距离，把每个对象归到距离它最近的聚类中心。如果全部对象被分配完，则每个聚类的聚类中心将根据现有的对象重新计算。直到满足某个终止条件。K均值聚类算法是一种无监督学习算法。具体算法实现如下：</p>
<p><img src="https://i.imgur.com/BHa0iXW.png" alt=""></p>
<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><pre><code>import numpy as np
import random
import matplotlib.pyplot as plt

#计算样本到中心的距离,采用欧式距离
def distEC(data,center,k):
    num = np.array(data)
    hang = num.shape[0] #数据个数
    lie = num.shape[1] #指标个数
    dist = [[0]*k for row in range(hang)]
    for i in range(hang):
        for l in range(k):
            s = 0
            for j in range(lie):
                s =  (center[l][j] - data[i][j])**2 + s
            dist[i][l] = s**0.5
    return dist

#根据计算得到的距离对样本分类
def Cluter(dist):
    num = np.array(dist)
    hang = num.shape[0] #数据个数
    k = num.shape[1] #中心个数
    clu = [[] for row in range(k)] #初始化存放样本标号的距离
    for i in range(hang):
        temp = dist[i][:].index(min(dist[i][:]))#求样本距离哪个聚类中心最近
        for j in range(k):#判断样本属于哪个距离中心
            if temp == j:
                clu[j].append(i)
                break
    return clu

#重新计算聚类中心

def clu_center(clu,data,k):
    for i in range(k):
        for j in range(2):#(np.array(data).shape[1]):
            temp = 0
            for k in range(len(clu[i])):#len(clu[i])为每个聚类中心样本的个数
                temp = data[clu[i][k]][j] + temp
            center[i][j] = temp / len(clu[i]) #重新计算得到的聚类中心
    return center

if __name__ == &quot;__main__&quot;:
    k =3#聚类中心个数
    data = [[0,0],[3,8],[2,2],[1,1],[5,3],[4,8],[6,3],[5,4],[6,4],[7,5]]
    #center = random.sample(data,k)#随机选取初始聚类中心
    center = [[0,0],[3,8],[2,2]]#固定初始聚类中心
    dist = distEC(data,center,k)#计算样本与聚类中心的距离
    clu = Cluter(dist)#聚类
    center1 = clu_center(clu,data,k)#重新计算聚类中心
    center2 = []
    while center1 != center2: #当聚类中心相同时，停止聚类
        dist = distEC(data,center1,k)
        clu = Cluter(dist)
        center2 = center1
        center1 = clu_center(clu,data,k)
        print(center1,center2)

    #画图
    plt.figure()
    for j in range(10):
        plt.plot(data[j][0],data[j][1],&apos;b.&apos;)
    for i in range(k):
        plt.plot(center1[i][0],center[i][1],&apos;ro&apos;)
    plt.xlabel(&apos;x&apos;)
    plt.ylabel(&apos;y&apos;)
    plt.show()
</code></pre><h3 id="2-5-2-ISODATA算法（迭代自组织的数据分析算法）"><a href="#2-5-2-ISODATA算法（迭代自组织的数据分析算法）" class="headerlink" title="2.5.2 ISODATA算法（迭代自组织的数据分析算法）"></a>2.5.2 ISODATA算法（迭代自组织的数据分析算法）</h3><p>K—均值算法比较简单，但它的自我调整能力也比较差。这主要表现在类别数不能改变，受代表点初始选择的影响也比较大。</p>
<p>ISODATA算法的功能与K—均值算法相比，在下列几方面有改进：</p>
<ul>
<li>可以改变类别数目。通过类别的合并与分裂来实现。</li>
<li>合并主要发生在某一类内样本个数太少的情况，或两类聚类中心之间距离太小的情况。为此设有最小类内样本数限制，以及类间中心距离参数。</li>
<li>分裂则主要发生在某一类别的某分量出现类内方差过大的现象，因而宜分裂成两个类别，以维持合理的类内方差。给出一个对类内分量方差的限制参数，用以决定是否需要将某一类分裂成两类。</li>
<li>由于算法有自我调整的能力，因而需要设置若干个控制用参数。，如聚类数期望值K、每次迭代允许合并的最大聚类对数L、及允许迭代次数I等。</li>
</ul>
<h2 id="2-6-聚类结果评价"><a href="#2-6-聚类结果评价" class="headerlink" title="2.6 聚类结果评价"></a>2.6 聚类结果评价</h2><p>可考虑用以下几个指标来评价聚类效果：</p>
<ul>
<li>聚类中心之间的距离：距离值大，通常可考虑分为不同类</li>
<li>聚类域中的样本数目：样本数目少且聚类中心距离远，可考虑是否为噪声</li>
<li>聚类域内样本的距离方差： 方差过大的样本可考虑是否属于这一类</li>
</ul>

      
    </div>
    
    
    

    
      <div>
        <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/images/wechat-qcode.jpg" alt="zxp wechat" style="width: 200px; max-width: 100%;"/>
    <div>欢迎关注微信公众号！</div>
</div>

      </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/WeChatpay.jpg" alt="zxp 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/Alipay.jpg" alt="zxp 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    zxp
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://zxpgo.github.io/2018/09/11/聚类分析/" title="模式识别(二)--聚类分析">https://zxpgo.github.io/2018/09/11/聚类分析/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tag/模式识别/" rel="tag"># 模式识别</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/09/11/模式识别绪论/" rel="next" title="模式识别绪论(一)">
                <i class="fa fa-chevron-left"></i> 模式识别绪论(一)
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/09/12/C++ Primer Plus--复合类型（四）/" rel="prev" title="C++ Primer Plus--复合类型（四）">
                C++ Primer Plus--复合类型（四） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zODgxNC8xNTM0Mg=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.gif"
                alt="zxp" />
            
              <p class="site-author-name" itemprop="name">zxp</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">176</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">48</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_25774883" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-globe"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/zxpgo/zxpgo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-globe"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.linkedin.com/feed/" target="_blank" title="LinkedIn">
                      
                        <i class="fa fa-fw fa-globe"></i>LinkedIn</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="1165772354@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://theme-next.iissnan.com" title="Next主题" target="_blank">Next主题</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://blog.rexking6.top" title="青爷博客" target="_blank">青爷博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://me.csdn.net/download/qq_25774883" title="CSDN下载" target="_blank">CSDN下载</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.livere.com/" title="来必力" target="_blank">来必力</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://tongji.baidu.com/web/welcome/login" title="百度统计" target="_blank">百度统计</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://leancloud.cn/" title="LeanCloud" target="_blank">LeanCloud</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ibruce.info/2015/04/04/busuanzi/" title="不蒜子" target="_blank">不蒜子</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://leetcode-cn.com/" title="LeetCode" target="_blank">LeetCode</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-距离聚类的概念"><span class="nav-number">1.</span> <span class="nav-text">2.1 距离聚类的概念</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#物以俱类"><span class="nav-number">1.1.</span> <span class="nav-text">物以俱类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#相似性"><span class="nav-number">1.2.</span> <span class="nav-text">相似性</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-相似度测量和距离准则"><span class="nav-number">2.</span> <span class="nav-text">2.2 相似度测量和距离准则</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-1-相似性测度"><span class="nav-number">2.1.</span> <span class="nav-text">2.2.1 相似性测度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-2-聚类准测"><span class="nav-number">2.2.</span> <span class="nav-text">2.2.2 聚类准测</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-基于距离阈值的聚类算法"><span class="nav-number">3.</span> <span class="nav-text">2.3 基于距离阈值的聚类算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-1-近邻聚类算法"><span class="nav-number">3.1.</span> <span class="nav-text">2.3.1 近邻聚类算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#问题："><span class="nav-number">3.1.1.</span> <span class="nav-text">问题：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#算法描述"><span class="nav-number">3.1.2.</span> <span class="nav-text">算法描述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#算法特点"><span class="nav-number">3.1.3.</span> <span class="nav-text">算法特点</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-2-最大最小距离算法"><span class="nav-number">3.2.</span> <span class="nav-text">2.3.2 最大最小距离算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#问题：-1"><span class="nav-number">3.2.1.</span> <span class="nav-text">问题：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#算法描述-1"><span class="nav-number">3.2.2.</span> <span class="nav-text">算法描述</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-层次聚类"><span class="nav-number">4.</span> <span class="nav-text">2.4 层次聚类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#算法描述-2"><span class="nav-number">4.0.1.</span> <span class="nav-text">算法描述</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5-动态聚类法"><span class="nav-number">5.</span> <span class="nav-text">2.5 动态聚类法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-1-K-均值算法"><span class="nav-number">5.1.</span> <span class="nav-text">2.5.1 K-均值算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#算法描述-3"><span class="nav-number">5.1.1.</span> <span class="nav-text">算法描述</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码"><span class="nav-number">6.</span> <span class="nav-text">代码</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-2-ISODATA算法（迭代自组织的数据分析算法）"><span class="nav-number">6.1.</span> <span class="nav-text">2.5.2 ISODATA算法（迭代自组织的数据分析算法）</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-6-聚类结果评价"><span class="nav-number">7.</span> <span class="nav-text">2.6 聚类结果评价</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div>
<script async src="https//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_pv" style='display:none'>
    本站总访问量 <span id="busuanzi_value_site_pv"></span> 
    <span class="post-meta-divider">|</span>
</span>
<span id="busuanzi_container_site_uv" style='display:none'>
    访问人数 <span id="busuanzi_value_site_uv"></span>
</span>
</div>


<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; 2018-8 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-"></i> Power by 
  </span>
  <span class="author" itemprop="copyrightHolder">zxp</span>
  
  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  




  
  <div id="lv-container" data-uid="MTAyMC8zODgxNC8xNTM0Mg==">
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
	</div>
  











  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("2AyV3DKioBSdoryrFLRohzjB-gzGzoHsz", "XynedcHyJCVCrTfbD4yYnodo");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "default";
      
          pbOptions.boxForm = "vertical";
      
          pbOptions.position = "top";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "box";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=5.1.4"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=5.1.4"></script>


  
  <script type="text/javascript" src="/js/src/exturl.js?v=5.1.4"></script>


  
  
  	 <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("2AyV3DKioBSdoryrFLRohzjB-gzGzoHsz", "XynedcHyJCVCrTfbD4yYnodo");</script>
<script>
function showTime(Counter) {
	var query = new AV.Query(Counter);
	$(".leancloud_visitors").each(function() {
		var url = $(this).attr("id").trim();
		query.equalTo("url", url);
		query.find({
			success: function(results) {
				if (results.length == 0) {
					var content = $(document.getElementById(url)).text() + ' 0';
					$(document.getElementById(url)).text(content);
					return;
				}
				for (var i = 0; i < results.length; i++) {
					var object = results[i];
					var content = $(document.getElementById(url)).text() + ' ' + object.get('time');
					$(document.getElementById(url)).text(content);
				}
			}
		});

	});
}

</script>
  
</body>
</html>
